<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    width="40%"
    :maskClosable="false"
    :title="t(`common.title.${type}`)"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
    <div class="cailiao" v-if="targetTypes != 30">
      <Divider orientation="left">已提交材料</Divider>
      <table
        v-show="resultAll && Object.keys(resultAll).length > 0"
        align="center"
        border="1"
        class="tableName"
        cellpadding="10"
        cellspacing="5"
      >
        <caption>
          <a-button
            style="float: right"
            preIcon="ant-design:download-outlined"
            type="primary"
            @click="handleBatchDownload(resultAll)"
          >
            下载全部上传材料
          </a-button>
        </caption>
        <thead>
          <tr>
            <th>文件名称</th>
            <th>上传时间</th>
            <th>预览材料</th>
            <th>下载材料</th>
            <th v-if="upLoadShow != '1'">删除材料</th>
          </tr></thead
        >
        <tbody>
          <tr v-for="(item, value) in resultAll" :key="value">
            <td>{{ item.originalFileName }}</td>
            <td>{{ item.createdTime }}</td>
            <td>
              <a-button
                preIcon="ant-design:eye-filled"
                class="anniu anniu-first"
                @click="handleView(item.url)"
                >预览</a-button
              ></td
            >
            <td
              ><a-button
                preIcon="ant-design:download-outlined"
                class="anniu"
                @click="handleDown(item.fileId)"
                >下载</a-button
              ></td
            >
            <td
              ><a-button
                v-if="upLoadShow != '1'"
                preIcon="ant-design:delete-outlined"
                class="anniu"
                v-hasAnyPermission="['taskInterim:materialOperation']"
                @click="batchDelete(item.id)"
                >删除</a-button
              ></td
            >
          </tr>
        </tbody>
      </table>
    </div>
    <div>
      <!-- <a-divider orientation="left">Left Text</a-divider> -->
      <Divider orientation="left">评分</Divider>
      <div>
        <div
          style="margin-bottom: 25px; margin-left: 30px; margin-top: 10px"
          v-if="targetTypes != 30"
        >
          自我评分
          <!-- -{{ showTrue }}--{{ upLoadShow }} -->
          <a-input
            v-model:value="zipingfenshu"
            style="width: 200px; margin-left: 5px"
            :disabled="true"
          />
          <RadioGroup
            v-model:value="value1"
            button-style="solid"
            style="margin-left: 10px"
            :disabled="true"
          >
            <RadioButton value="10">未完成</RadioButton>
            <RadioButton value="20">进行中</RadioButton>
            <RadioButton value="30">已完成</RadioButton>
          </RadioGroup>
        </div>
        <div style="margin-bottom: 25px; margin-left: 30px" v-if="targetTypes != 30">
          完成情况
          <!-- v-show="markResultVOList.length !== 0" -->
          <a-textarea
            v-model:value="zipingfenshuneirong"
            style="width: 450px; margin-left: 5px"
            :auto-size="{ minRows: 2, maxRows: 6 }"
            :disabled="true"
          />
        </div>
        <div style="margin-bottom: 25px; margin-left: 30px; margin-top: 10px">
          最终评分
          <!-- :disabled="pinweifenshu.value == null" -->
          <a-input
            v-model:value="pinweifenshu"
            style="width: 200px; margin-left: 5px"
            :disabled="true"
          />
          <RadioGroup
            v-model:value="value2"
            style="margin-left: 10px"
            button-style="solid"
            :disabled="true"
          >
            <RadioButton value="10">未完成</RadioButton>
            <RadioButton value="20">进行中</RadioButton>
            <RadioButton value="30">已完成</RadioButton>
          </RadioGroup>
        </div>
        <div style="margin-bottom: 25px; margin-left: 30px">
          评分依据
          <a-textarea
            v-model:value="pinweifenshuneirong"
            style="width: 450px; margin-left: 5px"
            :disabled="true"
            :auto-size="{ minRows: 2, maxRows: 6 }"
          />
        </div>
      </div>
      <div style="margin-left: 40px">
        <a-button
          type="primary"
          v-hasAnyPermission="['taskInterim:assessMark']"
          style="margin-left: 40px"
          @click="pingfentanchuang"
          v-if="targetTypes != 30"
          >我要评分</a-button
        >
        <a-button
          type="primary"
          v-hasAnyPermission="['taskInterim:markOperation']"
          style="margin-left: 40px"
          @click="zipingtanchuang"
          v-if="targetTypes != 30 && !isExpiration && state"
          >我要自评</a-button
        >
        <a-button
          v-if="upLoadShow != '1' && (isExpiration || !state)"
          type="default"
          :disabled="true"
          >自评时间已截止</a-button
        >
      </div>
    </div>
    <!-- <BasicForm @register="registerGroup" @submit="handleSubmit" /> -->
    <pinfenEdit @register="pinfenModal" @success="pinfenSuccess" />
    <zipingEdit @register="zipingModal" @success="zipingSuccess" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { Divider, Radio } from 'ant-design-vue';
  import { defineComponent, ref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ActionEnum, FileBizTypeEnum } from '/@/enums/commonEnum';
  import { listByBizId } from '/@/api/lamp/file/upload';
  import { editFormSchema } from './defApplication.data';
  import { UploadgetDetailById, downloadFiles, remove } from '/@/api/basic/import/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadFile } from '/@/utils/lamp/common';
  // import { number } from 'vue-types';
  import { useUserStore } from '/@/store/modules/user';
  import pinfenEdit from './pinfenEdit.vue';
  import zipingEdit from './zipingEdit.vue';
  import { useModal } from '/@/components/Modal';
  export default defineComponent({
    name: 'BasicApplicationEdit',
    components: {
      BasicDrawer,
      BasicForm,
      Divider,
      RadioGroup: Radio.Group,
      RadioButton: Radio.Button,
      pinfenEdit,
      zipingEdit,
    },
    props: {
      foundId: {
        type: String as PropType<string>,
        required: true,
      },
      opednext: {
        type: [Boolean] as PropType<boolean>,
        default: true,
      },
    },
    emits: ['success', 'register'],
    setup(props, { emit }) {
      // console.log(props.opednext);
      const [pinfenModal, { openModal: openPingfen }] = useModal();
      const [zipingModal, { openModal: openZiping }] = useModal();
      const { t } = useI18n();
      const type = ref<ActionEnum>(ActionEnum.VIEW);
      const value1 = ref<string>('10');
      const value2 = ref<string>('10');
      let showTrue = ref(true);

      let resultAll: any = ref();
      let opednext = ref(Boolean);
      let markResultVOList: any = ref([]);
      let zipingfenshu: any = ref();
      let pinweifenshu: any = ref();
      let zipingfenshuneirong: any = ref();
      let pinweifenshuneirong: any = ref();
      let result: any = ref();
      let targetTypes: any = ref();
      let detailResule: any = ref();
      const userStore = useUserStore();
      const upLoadShow = userStore.getTenantId;
      const { createMessage } = useMessage();
      let results: any = ref();
      let isExpiration = ref(true);
      let state = ref(true);
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        await resetSchema(editFormSchema(type));
        await resetFields();

        setDrawerProps({ confirmLoading: false });
        type.value = data?.type;

        // 赋值
        const record = { ...data?.record };
        const opednexts = { ...data };
        opednext.value = opednexts.opednext;
        results.value = record;
        showTrue.value = record.state;
        let arr = {
          id: [record.id][0],
          otherId: [record.tenantId][0],
        };
        await UploadDel(arr);
        const appendixIcons = await listByBizId({
          bizId: record.id,
          bizType: FileBizTypeEnum.DEF_APPLICATION_LOGO,
        });
        record.appendixIcon = appendixIcons?.[0]?.id;
        // listAll = record;
        await setFieldsValue(record);
        // return opednext;
      });
      const [registerForm, { setFieldsValue, resetFields, resetSchema }] = useForm({
        labelWidth: 100,
        schemas: editFormSchema(type),
        showActionButtonGroup: false,
        // disabled: showTrue,
        disabled: true,
        baseColProps: { span: 24 },
        actionColOptions: {
          span: 23,
        },
      });

      async function UploadDel(value) {
        result.value = await UploadgetDetailById(value);
        detailResule.value = result.value;
        targetTypes.value = result.value.targetType;
        resultAll.value = result.value.materialResultVOList;
        isExpiration.value = result.value.isExpiration;
        state.value = result.value.state;

        markResultVOList.value = result.value.markResultVOList;
        if (result.value.markResultVOList.length > 1) {
          for (var i = 0; i < markResultVOList.value.length; i++) {
            if (markResultVOList.value[i].isDefault) {
              value1.value = markResultVOList.value[i].targetState;
              zipingfenshu.value = markResultVOList.value[i].score;
              zipingfenshuneirong.value = markResultVOList.value[i].remarks;
            } else {
              // pinweishow.value = true;
              pinweifenshu.value = markResultVOList.value[i].score;
              pinweifenshuneirong.value = markResultVOList.value[i].remarks;

              value2.value = markResultVOList.value[i].targetState;
            }
          }
        } else if (result.value.markResultVOList.length == 1) {
          if (markResultVOList.value[0].isDefault) {
            // zipinshow.value = true;
            zipingfenshu.value = markResultVOList.value[0].score;
            zipingfenshuneirong.value = markResultVOList.value[0].remarks;

            value1.value = markResultVOList.value[0].targetState;
            // pinweishow.value = false;
            value2.value = '10';
            pinweifenshu.value = undefined;
            pinweifenshuneirong.value = undefined;
          } else {
            // pinweishow.value = true;
            pinweifenshu.value = markResultVOList.value[0].score;
            pinweifenshuneirong.value = markResultVOList.value[0].remarks;
            value2.value = markResultVOList.value[0].targetState;
            value1.value = '10';
            // zipinshow.value = false;
            zipingfenshu.value = undefined;
            zipingfenshuneirong.value = undefined;
          }
        } else {
          zipingfenshu.value = undefined;
          pinweifenshu.value = undefined;
          zipingfenshuneirong.value = undefined;
          pinweifenshuneirong.value = undefined;
          value1.value = '10';
          value2.value = '10';
          // zipinshow.value = false;
          // pinweishow.value = false;
        }
        return {
          result,
          resultAll,
          markResultVOList,
          zipingfenshu,
          pinweifenshu,
          zipingfenshuneirong,
          pinweifenshuneirong,
          targetTypes,
          isExpiration,
          state,
          value1,
          value2,
          // zipinshow,
          // pinweishow,
        };
      }
      // async function names(params) {
      //   const resule = await save(params);
      //   console.log(resule);
      // }
      function handleSubmit() {
        closeDrawer();
        emit('success');
        // try {
        //   setDrawerProps({ confirmLoading: true });
        //   const params = await validate();
        //   if (
        //     params.score == '' ||
        //     params.remarks == '' ||
        //     Number(params.targetScore) < Number(params.score)
        //   ) {
        //     createMessage.error('评分值不能大于项目原定分值，请重新输入');
        //   } else {
        //     // console.log(zipingfenshu.value, zipingfenshuneirong.value, '自评');
        //     // console.log(pinweifenshu.value, pinweifenshuneirong.value, '评委');
        //     const arr = {
        //       targetId: params.id,
        //       score: pinweifenshu.value,
        //       remarks: pinweifenshuneirong.value,
        //       otherId: results.value.tenantId,
        //       targetState: value2.value,
        //     };
        //     const arrs = {
        //       targetId: params.id,
        //       score: zipingfenshu.value,
        //       remarks: zipingfenshuneirong.value,
        //       targetState: value1.value,
        //     };
        //     if (pinweifenshu.value !== undefined && pinweifenshuneirong.value !== undefined) {
        //       // console.log(detailResule.value, '88888888888888888888888');
        //       if (detailResule.value.markResultVOList) {
        //         for (var i = 0; i < markResultVOList.value.length; i++) {
        //           if (!markResultVOList.value[i].isDefault) {
        //             if (markResultVOList.value[i].score !== undefined) {
        //               const arrchage = {
        //                 targetId: params.id,
        //                 score: pinweifenshu.value,
        //                 remarks: pinweifenshuneirong.value,
        //                 otherId: results.value.tenantId,
        //                 targetState: value2.value,
        //                 id: markResultVOList.value[i].id,
        //               };
        //               await taskInterimMarkPUT(arrchage);
        //             }
        //           }
        //         }
        //       } else {
        //         await taskInterimMark(arr);
        //       }
        //     } else if (
        //       zipingfenshu.value !== undefined &&
        //       zipingfenshuneirong.value !== undefined
        //     ) {
        //       // await taskInterimMark(arrs);
        //       if (detailResule.value.markResultVOList) {
        //         for (var i = 0; i < markResultVOList.value.length; i++) {
        //           if (markResultVOList.value[i].isDefault) {
        //             if (markResultVOList.value[i].score !== undefined) {
        //               const arrchages = {
        //                 targetId: params.id,
        //                 score: zipingfenshu.value,
        //                 remarks: zipingfenshuneirong.value,
        //                 targetState: value1.value,
        //                 id: markResultVOList.value[i].id,
        //               };
        //               await taskInterimMarkPUT(arrchages);
        //             }
        //           }
        //         }
        //       } else {
        //         await taskInterimMark(arrs);
        //       }
        //     }

        //     closeDrawer();
        //     emit('success');
        //   }
        // } finally {
        //   setDrawerProps({ confirmLoading: false });
        // }
      }
      // 预览
      function handleView(value) {
        window.open(value, '_blank');
      }
      // 下载
      async function handleDown(id: string[]) {
        // const response = await download(id);
        const response = await downloadFiles(id);
        downloadFile(response);
        createMessage.success(t('common.tips.downloadSuccess'));
      }
      async function handleBatchDownload(values: any[]) {
        const ids: any[] = [];
        for (var i = 0; i < values.length; i++) {
          ids.push(values[i].fileId);
        }
        const response = await downloadFiles(ids);
        downloadFile(response);
        createMessage.success(t('common.tips.downloadSuccess'));
      }
      // 删除
      async function batchDelete(id: string[]) {
        const delId = [id];
        await remove(delId);
        createMessage.success(t('common.tips.deleteSuccess'));
        closeDrawer();
        // let arr = {
        //   id: [record.id][0],
        //   otherId: [record.tenantId][0],
        // };
        // await UploadgetDetailById(arr)
      }
      function pingfentanchuang() {
        openPingfen(true, {
          detailResule,
          results,
        });
      }
      function zipingtanchuang() {
        openZiping(true, {
          detailResule,
        });
      }
      function pinfenSuccess() {
        closeDrawer();
      }
      function zipingSuccess() {
        closeDrawer();
      }
      return {
        result,
        pinfenModal,
        zipingModal,
        pingfentanchuang,
        zipingtanchuang,
        pinfenSuccess,
        zipingSuccess,
        t,
        registerDrawer,
        registerForm,
        type,
        handleSubmit,
        UploadDel,
        resultAll,
        handleView,
        handleDown,
        handleBatchDownload,
        batchDelete,
        remove,
        upLoadShow,
        markResultVOList,
        zipingfenshu,
        pinweifenshu,
        zipingfenshuneirong,
        pinweifenshuneirong,
        showTrue,
        targetTypes,
        isExpiration,
        state,
        value1,
        value2,
        // zipinshow,
        // pinweishow,
      };
    },
  });
</script>
<style lang="less" scoped>
  .names {
    line-height: 40px;
    height: 40px;
  }

  .anniu {
    margin-right: 5px;
  }

  .anniu-first {
    margin-left: 20px;
  }

  .cailiao-title {
    display: flex;
  }

  .tableName {
    border: 1 solid #000;
  }
</style>
